#include <iostream>
#include <string>
#include <memory>
#include <unistd.h>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"
using namespace std;


string ServerHandler(string &message)
{
    HttpRequest req;
    if(!req.Deserialize(message)) exit(1);
    req.Debug();
    // cout << message << endl;


    HttpResponse rep;
    string response = rep.Response(req);

    return response;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "Incorrect use, correct use:\n"
             << "    " << argv[0] << " port" << endl;
    }
    uint16_t localport = stoi(argv[1]);
    unique_ptr<TcpServer> up(new TcpServer(localport, ServerHandler));

    up->Loop();

    return 0;
}


// string ServerHandler(string& message, Protocol pro)
// {
//     unique_ptr<Factory> ft(new Factory());
//     while (true)
//     {
//         // 1. 接收有效载荷
//         char resquest[1024];
//         ssize_t n = recv(sock->GetSockFd(), resquest, sizeof(resquest), 0);
//         resquest[n] = 0;
//         // 2. 有效载荷反序列化
//         shared_ptr<Calculator::Request> req = ft->BuildCalRequest();
//         req->Deserialize(resquest);
//         // 3. 处理数据，并构建响应
//         shared_ptr<Calculator::Response> rep = req->Result();
//         // 4. 响应序列化
//         string response;
//         rep->Serialize(response);
//         // 5. 发送响应序列化
//         send(sock->GetSockFd(), response.c_str(), response.size(), 0);
//         sleep(1);
//     }
// }